﻿using BlogApi.Data;
using BlogApi.Models;
using BlogApi.ModelsDto;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace BlogApi.Services
{
    public class CommentRepository:ICommentRepository
    {
        private readonly BlogDbContext _context;

        public CommentRepository(BlogDbContext context)
        {
            _context = context ?? throw new ArgumentNullException(nameof(context));
        }
        public async Task<IEnumerable<Comment>> GetCommentsAsync()
        {
            return await _context.Comments_table.ToListAsync();
        }
        public async Task<IEnumerable<Comment>> GetCommentsByPageAsync(int pageSize, int pageIndex)
        {
            return await _context.Comments_table.Skip(pageSize * pageIndex).Take(pageSize).ToListAsync();
        }
        public async Task<Comment> GetCommentAsync(Guid commentId)
        {
            if (commentId == Guid.Empty)
            {
                throw new ArgumentNullException(nameof(commentId));
            }
            return await _context.Comments_table.FirstOrDefaultAsync(comment => comment.Id == commentId);
        }
        public async Task<CommentAndUserInfoDto> GetCommentAndUserInfoAsync(Guid commentId)
        {
            return await _context.Comments_table.Where(c => c.Id == commentId).Include(c => c.User).Select(c => new CommentAndUserInfoDto()
            {
                UserId = c.UserId,
                UserNickName = c.User.NickName,
                UserImg = c.User.UserImageUrl,
                CommentId = c.Id,
                Date = c.CreateTime.ToString("yyyy-MM-dd HH:mm"),
                CommentText = c.Content
            }).FirstAsync();
        }
        public async Task<IEnumerable<CommentAndUserInfoDto>> GetArticleCommentAndUserInfoDtosByPageAsync(Guid articleId, int pageSize, int pageIndex)
        {
            if (articleId == Guid.Empty)
            {
                throw new ArgumentNullException(nameof(articleId));
            }
            return await _context.Comments_table.Where(comment => comment.ArticleId == articleId).Include(comment => comment.User).OrderByDescending(comment => comment.CreateTime).Skip(pageSize * pageIndex).Take(pageSize).Select(comment => new CommentAndUserInfoDto()
            {
                UserId = comment.UserId,
                UserNickName = comment.User.NickName,
                UserImg = comment.User.UserImageUrl,
                CommentId = comment.Id,
                Date = comment.CreateTime.ToString("yyyy-MM-dd HH:mm"),
                CommentText = comment.Content
            }).ToListAsync();
        }
        public void AddComment(Comment comment)
        {
            if (comment == null)
            {
                throw new ArgumentNullException(nameof(comment));
            }
            _context.Comments_table.Add(comment);
        }
        public void UpdateComment(Comment comment)
        {
            //由于数据库上下文会对实体进行跟踪 所以这句话可以不写
            //_context.Entry(comment).State = EntityState.Modified;
        }
        public void DeleteComment(Comment comment)
        {
            if (comment == null)
            {
                throw new ArgumentNullException(nameof(comment));
            }
            _context.Comments_table.Remove(comment);
        }
        public async Task<bool> CommentExistsAsync(Guid commentId)
        {
            if (commentId == Guid.Empty)
            {
                throw new ArgumentNullException(nameof(commentId));
            }
            return await _context.Comments_table.AnyAsync(comment => comment.Id == commentId);
        }
        public async Task<bool> SaveAsync()
        {
            return await _context.SaveChangesAsync() >= 0;
        }
    }
}
